package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    private static final Logger log = LoggerFactory.getLogger(SetmealServiceImpl.class);
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public Result<Map<String, Object>> queryByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //查询套餐、分页并查询分类名称
        List<SetmealVO> setmealList = setmealMapper.queryByPageWithCategory(setmealPageQueryDTO);
        //创建map封装结果
        HashMap<String, Object> result = new HashMap<>();
        result.put("total",setmealList.size());
        result.put("records",setmealList);
        //返回结果
        return Result.success(result);
    }


    /**
     * 套餐新增
     * @param setmealVO
     * @return
     */
    @Transactional
    @Override
    public Result saveSetmeal(SetmealVO setmealVO) {
        //创建套餐对象并属性拷贝
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        //新增套餐
        setmealMapper.insert(setmeal);
        //新增套餐与分类关联
        if(setmealVO.getSetmealDishes()!=null && setmealVO.getSetmealDishes().size()>0){
            setmealDishMapper.insertBatch(setmeal.getId(),setmealVO.getSetmealDishes());
        }
        //返回结果
        return Result.success();
    }


    /**
     * 套餐主键查询
     * @param id
     * @return
     */
    @Override
    public Result<SetmealVO> queryBySetmealWithSetmealDish(Long id) {
        //查询套餐和分类
        SetmealVO setmealVO = setmealMapper.queryBySetmealWithCategory(id);
        //查询套餐与菜品关联
        List<SetmealDish> setmealDishes = setmealDishMapper.queryBySetmealId(id);
        //封装数据
        setmealVO.setSetmealDishes(setmealDishes);
        //返回结果
        return Result.success(setmealVO);
    }


    /**
     * 套餐修改
     * @param setmealVO
     * @return
     */
    @Override
    public Result updateSetmeal(SetmealVO setmealVO) {
        //创建套餐对象并属性拷贝
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        //修改套餐
        setmealMapper.update(setmeal);
        //删除套餐与菜品关联
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //新增套餐与菜品关联
        if(setmealVO.getSetmealDishes()!=null && !setmealVO.getSetmealDishes().isEmpty()){
            setmealDishMapper.insertBatch(setmeal.getId(),setmealVO.getSetmealDishes());
        }
        //返回结果
        return Result.success();
    }

    /**
     * 套餐启用或禁用
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result startOrStop(Integer status, Integer id) {
        //创建套餐对象并设置属性
        Setmeal setmeal = new Setmeal();
        setmeal.setId(Long.valueOf(id));
        setmeal.setStatus(status);
        //修改状态
        setmealMapper.update(setmeal);
        //返回结果
        return Result.success();
    }


    /**
     * 套餐批量删除
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public Result deleteSetmeal(List<Integer> ids) {
        //启售状态不能删除
        for (Integer id : ids) {
            SetmealVO setmealVO = setmealMapper.queryBySetmealWithCategory(Long.valueOf(id));
            if(setmealVO.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐中关联的菜品
        setmealDishMapper.deleteBySetmealIds(ids);
        //删除套餐
        setmealMapper.deleteByIds(ids);
        //返回结果
        return Result.success();
    }

    /**
     * 查询套餐内菜品
     * @param setmealId
     * @return
     */
    @Override
    public Result<List<DishItemVO>> queryBySetmealWithDish(Long setmealId) {
        //查询套餐内菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.queryBySetmealId(setmealId);
        //通过Stream
        List<DishItemVO> result = setmealDishes.stream().map(item -> {
            Dish dish = dishMapper.queryById(Math.toIntExact(item.getDishId()));
            DishItemVO dishItemVO = new DishItemVO();
            dishItemVO.setName(dish.getName());
            dishItemVO.setDescription(dish.getDescription());
            dishItemVO.setImage(dish.getImage());
            dishItemVO.setCopies(item.getCopies());
            return dishItemVO;
        }).collect(Collectors.toList());
        return Result.success(result);
    }


    /**
     * 分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public Result<List<Setmeal>> querySetmealList(Integer categoryId) {
        List<Setmeal> setmealList = setmealMapper.queryByCategoryIdList(categoryId);
        ArrayList<Setmeal> result = new ArrayList<>();
        setmealList.forEach(item ->{
            if(item.getStatus() == StatusConstant.ENABLE){
                result.add(item);
            }
        });
        return Result.success(result);
    }
}
